import router from './router';
import store from './store';
import { isRelogin } from '@/utils/request';
import { getToken, getWxChatUrl } from '@/utils/auth';
import { Notify } from 'vant';
import sha1 from 'sha1'


const whiteList = ['/', '/role', '/login', '/nologin-yjjy', '/bgt']

router.beforeEach((to, from, next) => {
  let {
    houseCode
  } = to.query;
  /* wx.config({
    debug: true,
    appId: "wx494c8117d3973fe6",
    jsApiList: ["checkJsApi", "onMenuShareTimeline", "onMenuShareAppMessage", "onMenuShareQQ", "onMenuShareWeibo", "onMenuShareQZone", "hideMenuItems", "showMenuItems", "hideAllNonBaseMenuItem", "showAllNonBaseMenuItem", "translateVoice", "startRecord", "stopRecord", "onVoiceRecordEnd", "playVoice", "onVoicePlayEnd", "pauseVoice", "stopVoice", "uploadVoice", "downloadVoice", "chooseImage", "previewImage", "uploadImage", "downloadImage", "getNetworkType", "openLocation", "getLocation", "hideOptionMenu", "showOptionMenu", "closeWindow", "scanQRCode", "chooseWXPay", "openProductSpecificView", "addCard", "chooseCard", "openCard"],
    nonceStr: "8462fa05-4ebc-4aeb-8c84-2ce1f58c6102",
    signature: '8d229f8a244ad466e61f2e042740baddab76c290',
    timestamp: '1725454831553',
    openTagList: ['wx-open-launch-app'] // 可选，需要使用的开放标签列表，例如['wx-open-launch-app']
  }) */
  if (getToken()) {
    /* has token*/
    if ((to.path === '/role' || to.path === '/') && !houseCode) {
      next('/nologin-yjjy')
    } else {
      if (store.getters.roles.length === 0) {
        isRelogin.show = true
        // 判断当前用户是否已拉取完user_info信息
        store.dispatch('GetInfo').then(() => {
          isRelogin.show = false
          next({ ...to, replace: true })
        }).catch(err => {
            store.dispatch('LogOut').then(() => {
              Notify({
                message: err,
                type: 'danger',
              })
              next({ path: '/role?houseCode=' + houseCode })
            })
          })
      } else {
        next()
      }
    }
  } else {
    // 没有token
    if (whiteList.indexOf(to.path) !== -1) {
      // 在免登录白名单，直接进入
      if ((to.path === '/role' || to.path === '/') && !houseCode) {
        next('/nologin-yjjy')
      } else {
        next()
      }
    } else {
      next('/role?houseCode=' + houseCode) // 否则全部重定向到登录页
    }
  }
})